package com.demo.spark.rdd;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.api.java.function.PairFunction;

import java.util.Arrays;
import java.util.Iterator;

public class RddFlatMap {
    public static void main(String[] args) {
        SparkConf sparkConf = new SparkConf();
        sparkConf.setAppName("rdd flat map");
        sparkConf.setMaster("local");
        JavaSparkContext javaSparkContext = new JavaSparkContext(sparkConf);
        javaSparkContext.setLogLevel("error");

        JavaRDD lines = javaSparkContext.parallelize(Arrays.asList("hello world", "hi"));
        JavaRDD words = lines.flatMap(
                new FlatMapFunction<String,String>(){
                    @Override
                    public Iterator<String> call(String lines) throws Exception{
                        return Arrays.asList(lines.split(" ")).iterator();
                    }
                });
        System.out.println(words.collect());
        System.out.println(words.first());

        javaSparkContext.stop();
    }
}
